﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using estudent.Models;

namespace estudent.Controllers
{
    public class VrsteVpisaController : Controller
    {
        estudentEnt db = new estudentEnt();
        string stara = null;
        //
        // GET: /VrsteVpisa/

        public ActionResult Index()
        {
            return View(db.VrsteVpisas.OrderBy(p => p.opisVrste).ToList());
        }

        //
        // GET: /VrsteVpisa/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /VrsteVpisa/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /VrsteVpisa/Create

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create([Bind(Exclude = "veljavnost")]VrsteVpisa ustvarjen)
        {
            try
            {
                if (!ModelState.IsValid)
                    return View();

                db.AddToVrsteVpisas(ustvarjen);
                db.SaveChanges();

                return RedirectToAction("Index");
            }
            catch
            {
                ModelState.AddModelError("idVrstaVpisa", "Napaka");
                return View();
            }
        }
        
        //
        // GET: /VrsteVpisa/Edit/5
 
        public ActionResult Edit(string id)
        {
            var uredi = (from p in db.VrsteVpisas
                         where p.idVrstaVpisa == id
                         select p).First();
            stara = id;

            return View(uredi);
        }

        //
        // POST: /VrsteVpisa/Edit/5

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit([Bind(Exclude = "veljavnost")]VrsteVpisa urejen)
        {
            if (urejen.idVrstaVpisa != stara && db.VrsteVpisas.Any(p => p.idVrstaVpisa == urejen.idVrstaVpisa))
                ModelState.AddModelError("idVrstaVpisa", "Obstaja");
            try
            {
                var original = (from p in db.VrsteVpisas
                                where p.idVrstaVpisa == urejen.idVrstaVpisa
                                select p).First();

                if (db.Vpisis.Any(p => p.idVrstaVpisa == original.idVrstaVpisa) && original.idVrstaVpisa != urejen.idVrstaVpisa)
                    ModelState.AddModelError("idVrstaVpisa", "Že v vpisu");

                db.ApplyCurrentValues(original.EntityKey.EntitySetName, urejen);
                db.SaveChanges();
 
                return RedirectToAction("Index");
            }
            catch
            {
                ModelState.AddModelError("idVrstaVpisa", "Unikat");
                return View();
            }
        }

        //
        // GET: /VrsteVpisa/Delete/5
 
        public ActionResult Delete(string id)
        {
            try
            {
                var orig = (from p in db.VrsteVpisas
                            where p.idVrstaVpisa == id
                            select p).First();
                var spremen = (from p in db.VrsteVpisas
                               where p.idVrstaVpisa == id
                               select p).First();

                if (orig.veljavnost != null)
                {
                    spremen.veljavnost = !orig.veljavnost;
                }
                else
                {
                    spremen.veljavnost = false;
                }

                db.ApplyCurrentValues(orig.EntityKey.EntitySetName, spremen);
                db.SaveChanges();

                return RedirectToAction("Index");
            }
            catch
            {
                return RedirectToAction("Index");
            }
        }

        //
        // POST: /VrsteVpisa/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
